<?php

/**
 * New account registration controller
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: RegisterController.php 38 2009-02-18 17:12:39Z monzee $
 */
class User_RegisterController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $action = $this->_helper->url('submit');
        $this->view->form = new User_RegisterForm(array('action' => $action));
    }

    public function submitAction()
    {
        $req = $this->_request;
        if (!$req->isPost()) {
            $this->_redirect('index');
            return;
        }

        $form = new User_RegisterForm();
        $password = $req->getPost('password');
        $form->passwordConfirm->addValidator('Identical', false, array($password));

        if ($form->isValid($req->getPost())) {
            $values = $form->getValues();
            $values['password'] = md5($form->getValue('password'));
            $values['group'] = 'Members';

            $user = new User_User();
            $user->create($values)
                 ->save();

            $this->_helper->flashMessenger
                ->setNamespace('user')
                ->addMessage('Welcome aboard, ' . $values['userName']);

            $this->_redirect($this->_helper->url('index', 'login'));
        } else {
            $action = $this->_helper->url('submit');
            $this->view->form = $form->setAction($action);

            $this->render('index');
        }
    }

}
